const { contextBridge, ipcRenderer } = require('electron/renderer')
const { serveUrl } = require('./config')

window.CallBridge = {
  serveUrl: serveUrl,
  invokeMethod: async function(e, a = '', b = ''){
    switch(e){
      case 'windowCtrl': 
        if(a === 0) ipcRenderer.send('minimize-window')
        if(a === 1) ipcRenderer.send('maximize-window')
        if(a === 2) ipcRenderer.send('close-window')
        if(a === 3) ipcRenderer.send('maximize-window')
        break;
      case 'reloadPage': ipcRenderer.send('reload-page'); break;
      case 'refreshAll': ipcRenderer.send('refresh-all'); break;
      case 'writeClipboard': ipcRenderer.send('write-clipboard', a); break;
      case 'selectFolder': return await ipcRenderer.invoke('select-folder');
      case 'selectFile': return await ipcRenderer.invoke('select-file', a);
      case 'readClipboard': return await ipcRenderer.invoke('read-clipboard');
      case 'getMemory': return await ipcRenderer.invoke('get-memory');
      case 'getHelp': return await ipcRenderer.invoke('get-help');
      case 'conn': return await ipcRenderer.send('conn');
      case 'quit': return await ipcRenderer.send('quit');
      // case 'url': return await ipcRenderer.send('url', a);
    }
  }
}

ipcRenderer.on('folder-path', (e, a, b) => {
  let elementName = document.getElementById(b);
  elementName.value = !!a ? a : ''
});

ipcRenderer.on('crash-file-path', (e, a) => {
  console.warn('crash-file-path:', a);
});

ipcRenderer.on('did-stop-loading', (e, a) => {
  console.warn('did-stop-loading:', a);
});

ipcRenderer.on('key-down', (e, r) => {
  // console.log('key-down:', r)
  // e.sender.send('key-down:', r)
  var keyDowndata = new CustomEvent('keyDownData', {
    detail: r,
  });
  document.dispatchEvent(keyDowndata);
});